<template>
	<div class="system-iotDeviceGroup-add">
		<!-- 添加或修改设备组对话框 -->
		<el-dialog v-model="isShowDialog" width="500px" :close-on-click-modal="false" :destroy-on-close="true">
			<template #header>
				<div>添加客户经理</div>
			</template>
			<el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
				<el-form-item label="用户账号：" prop="account">
					<el-input v-model="formData.account" placeholder="请输入用户账号" />
				</el-form-item>
				<el-form-item label="用户密码：" prop="pwd">
					<el-input v-model="formData.pwd" type="password"  show-password  placeholder="请输入用户密码" />
				</el-form-item>
				<el-form-item label="用户昵称：" prop="nickName">
					<el-input v-model="formData.nickName" placeholder="请输入用户昵称" />
				</el-form-item>
				<el-form-item label="手机号：" prop="phone">
					<el-input v-model="formData.phone" placeholder="请输入手机号" />
				</el-form-item>
				<el-form-item label="状态：" prop="status">
					<el-radio-group v-model="formData.status">
						<el-radio label="ENABLED">正常</el-radio>
						<el-radio label="DISABLED">停用</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="onCancel">取 消</el-button>
					<el-button type="primary" @click="onSubmit" :loading="loading">确 定</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>
<script lang="ts">
import { reactive, toRefs, defineComponent, ref, unref } from 'vue';
import { ElMessage, } from 'element-plus';
import { addManagerApi } from '@/api/customerManagerManage/customerManager'
export default defineComponent({
	name: 'customerManagerAdd',
	components: {},
	// props: {},
	setup(props, { emit }) {
		const formRef = ref<HTMLElement | null>(null);
		const menuRef = ref();
		const state = reactive({
			loading: false,
			isShowDialog: false,
			formData: {
				account: undefined,
				nickName: undefined,
				phone: undefined,
				status: undefined,
				pwd:undefined,
			},
			// 表单校验
			rules: {
				account: [{ required: true, message: '用户账号不能为空', trigger: 'blur' }],
				pwd: [{ required: true, message: '用户密码不能为空', trigger: 'blur' }],
				nickName: [{ required: true, message: '用户昵称不能为空', trigger: 'blur' }],
				status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
				phone: [
					{ required: true, message: '手机号不能为空', trigger: 'blur' },
					{
						pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
						message: "请输入正确的手机号码",
						trigger: "blur"
					}
				]
			},
		});
		// 打开弹窗
		const openDialog = () => {
			resetForm();
			state.isShowDialog = true;
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 提交
		const onSubmit = () => {
			const formWrap = unref(formRef) as any;
			if (!formWrap) return;
			formWrap.validate((valid: boolean) => {
				if (valid) {
					state.loading = true;
					//添加
					addManagerApi(state.formData)
						.then(() => {
							ElMessage.success('添加成功');
							closeDialog(); // 关闭弹窗
							emit('getList');
						})
						.finally(() => {
							state.loading = false;
						});
				}
			});
		};
		const resetForm = () => {
			state.formData = {
				account: undefined,
				nickName: undefined,
				phone: undefined,
				status: 'ENABLED',
				pwd:undefined,
			};
		};
		return {
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			resetForm,
			menuRef,
			formRef,
			...toRefs(state),
		};
	},
});
</script>
<style scoped></style>
